#pragma once
#include<vector>
#include<unordered_map>
#include"Timestamp.h"

namespace wjc {
    class Channel;
    using  ChannelList = std::vector<Channel*>;

    enum State {
        kNew,
        kAdded,
        kDeleted,
    };
    class Core;
    class Poller
    {
    public:

        Poller(Core* core);
        virtual ~Poller();

        /// Polls the I/O events.
        /// Must be called in the loop thread.
        virtual Timestamp poll(int timeoutMs, ChannelList* activeChannels) = 0;

        /// Changes the interested I/O events.
        /// Must be called in the loop thread.
        virtual void updateChannel(Channel* channel) = 0;

        /// Remove the channel, when it destructs.
        /// Must be called in the loop thread.
        virtual void removeChannel(Channel* channel) = 0;

        virtual bool hasChannel(Channel* channel) const;

        static Poller* newDefaultPoller(Core* core);

        void assertInLoopThread() const;

    protected:
        typedef std::unordered_map<int, Channel*> ChannelMap;
        ChannelMap channels_;

    private:
        Core* ownerCore_;
    };
}
